package com.it.springbootjpa.service.Impl;

import com.it.springbootjpa.dao.ContractDao;
import com.it.springbootjpa.domain.Contract;
import com.it.springbootjpa.query.ContractQury;
import com.it.springbootjpa.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class ContractServiceImpl implements ContractService {
    @Autowired
    ContractDao contractDao;
    @Override
    public void save(Contract contract) {
        contractDao.save(contract);
    }

    @Override
    public void update(Contract contract) {
    contractDao.save(contract);
    }

    @Override
    public void delete(Contract contract) {
    contractDao.delete(contract);
    }

    @Override
    public List<Contract> findAll() {
        return contractDao.findAll();
    }

    @Override
    public List<Contract> findByCondition(ContractQury contractQury) {
        if (contractQury.getContractNum()!=null&&contractQury.getContractType()!=null&&contractQury.getName()!=null){
            return contractDao.findAllByContractNumAndContractTypeAndName(String.valueOf(contractQury.getContractNum()),contractQury.getContractType(),contractQury.getName());
        }
        else if(contractQury.getContractStatus()!=null){
            return contractDao.findByContractStatus(contractQury.getContractStatus());
        }
        else if(contractQury.getContractNum()!=null){
            return contractDao.findByPage(String.valueOf(contractQury.getContractNum()), contractQury.getBeginIndex(), contractQury.getPageSize());
        }
        return null;
    }
}
